<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键词" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="单位/服务商搜索"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 250px"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        label="被授权机构"
        align="center"
        prop="companyName"
        width="150"
      />
      <el-table-column label="API Key" align="center" prop="token" />
      <el-table-column
        label="备注说明"
        align="center"
        prop="remark"
      />
      <el-table-column label="状态" align="center" prop="status" width="150">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-text="启用"
            :active-value="1"
            :inactive-value="0"
            inactive-text="禁用"
            @change="handleStatusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column
        label="最后更新时间"
        align="center"
        prop="updateTime"
        width="180"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOpen(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditAuthorization ref="EditAuthorization" @refresh="getList" />
  </div>
</template>

<script>
import { machineAuthorizeList, deleteMachineAuthorize, updateMachineAuthorizeStatus } from "@/api/order/waitMedicine.js";
import EditAuthorization from './editAuthorization.vue'

export default {
  name: "Dispenser",
  components: {
    EditAuthorization
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: "",
      },
      // 总条数
      total: 0,
      dataList: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      machineAuthorizeList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(row) {
      this.$refs.EditAuthorization.openModal(row);
    },
    handleStatusChange(row){
      updateMachineAuthorizeStatus({id: row.id, status: row.status}).then(()=>{
        this.$message.success("状态修改成功");
      })
    },
    handleDelete(id) {
      deleteMachineAuthorize({id}).then(()=>{
        this.$message.success("删除成功");
        this.getList();
      })
    }
  },
};
</script>

<style lang="scss" scoped></style>
